﻿using System;
using System.Collections.Generic;
using System.Linq;
using DevelopTool.Bll.LocalDbBlls;
using DevelopTool.Common;
using DevelopTool.Model.CommonModels;
using DevelopTool.Model.DbModels;
using DevelopTool.Model.Enums;
using DevelopTool.Model.LuckDrawModels;
using DevelopTool.Model.NoteModels;
using LiteDB;
using Sunny.UI;

namespace DevelopTool.Bll.LuckDrawBlls
{
    /// <summary>
    ///抽奖
    /// </summary>
    public class LuckDrawBll : BaseBll
    {
        public LuckDrawBll()
        {
            using (var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                var col = db.GetCollection<LuckDraw>();
                col.EnsureIndex(x => x.NodeName);
                col.EnsureIndex(x => x.NodePid);
                col.EnsureIndex(x => x.NodeType);
                col.EnsureIndex(x => x.CreateTime);
                col.EnsureIndex(x => x.Id);
            }
        }

        public List<LuckDraw> GetList(MenuNodeType nodeType = MenuNodeType.Unknown)
        {
            using (var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                var models = db.GetCollection<LuckDraw>().FindAll();

                if(nodeType != MenuNodeType.Unknown)
                {
                    models = models.Where(it => it.NodeType == nodeType);
                }

                return models.OrderByDescending(it => it.CreateTime).ToList();
            }
        }




        /// <summary>
        /// 根据内容关键字查询到id集合
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public List<LuckDraw> GetListByText(string text)
        {
            using (var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                List<LuckDraw> noteList = new List<LuckDraw>();

                var col = db.GetCollection<LuckDraw>();

                var luckDrawIdList = col.Find(it => !string.IsNullOrEmpty(it.Text) && it.Text.GZipDecompress().IndexOf(text, StringComparison.OrdinalIgnoreCase) > -1).Select(it => it.Id).ToList();
                foreach (int luckDrawId in luckDrawIdList)
                {
                    noteList.Add(col.FindOne(it => it.Id == luckDrawId && it.NodeType == MenuNodeType.Node));//只查询节点，不要目录
                }

                return noteList;
            }
        }



        /// <summary>
        /// 根据id获取对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public LuckDraw GetById(int id)
        {
            using (var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                var col = db.GetCollection<LuckDraw>();

                var luckDraw = col.FindOne(it => it.Id == id);

                //解压缩内容
                if (!string.IsNullOrEmpty(luckDraw.Text))
                    luckDraw.Text = luckDraw.Text.GZipDecompress();

                return luckDraw;

            }
        }


        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="note"></param>
        /// <returns></returns>
        public LuckDraw Save(LuckDraw luckDraw)
        {
            using (var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                var col = db.GetCollection<LuckDraw>();

                //压缩内容
                if (!string.IsNullOrEmpty(luckDraw.Text))
                    luckDraw.Text = luckDraw.Text.GZipCompress();

                luckDraw.UpdateTime = DateTime.Now;
                col.Upsert(luckDraw);

                return col.FindById(luckDraw.Id);
            }
        }


        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Delete(int id)
        {
            using (var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                var col = db.GetCollection<LuckDraw>();

                //查询是否有子节点，若有子节点，则不删除。
                if (col.Exists(it => it.NodePid == id)) return false;

                //删除
                var result = col.Delete(id);

                return result;
            }
        }

        public void SetRemark(int id,string remark)
        {
            using(var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                var col = db.GetCollection<LuckDraw>();

                var model = col.FindOne(it => it.Id == id);

                model.Remark = remark;
                model.UpdateTime = DateTime.Now;

                col.Update(model);
            }
        }

        public ManageMenu Get(int id)
        {
            using(var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                return db.GetCollection<LuckDraw>().FindOne(it => it.Id == id);
            }
        }

    }
}
